package com.ssy.lingxi.system.service.impl;

import com.ssy.lingxi.common.constant.member.MemberLevelTypeEnum;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.PageModel;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.api.feign.paas.BaseLevelRightCreditControllerFeign;
import com.ssy.lingxi.member.merchant.api.model.vo.paas.BaseLevelRuleStatusVO;
import com.ssy.lingxi.system.dao.MemberPromotionRuleRepository;
import com.ssy.lingxi.system.dto.request.MemberPromotionRuleUpdateRequest;
import com.ssy.lingxi.system.dto.response.MemberPromotionRuleResponse;
import com.ssy.lingxi.system.entity.MemberPromotionRule;
import com.ssy.lingxi.system.service.MemberPromotionRuleService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author wrc
 */
@Service
public class MemberPromotionRuleServiceImpl implements MemberPromotionRuleService {

    @Resource
    MemberPromotionRuleRepository memberPromotionRuleRepository;

    @Resource
    private BaseLevelRightCreditControllerFeign baseLevelRightCreditControllerFeign;

    @Override
    public Wrapper<PageData<MemberPromotionRuleResponse>> memberPromotionRuleList(PageModel request) {
        Page<MemberPromotionRule> pageList = memberPromotionRuleRepository.findAll(PageRequest.of(request.getCurrent(), request.getPageSize(), Sort.by("id").ascending()));
        return Wrapper.success(new PageData<>(pageList.getTotalElements(), pageList.getContent().stream().map(memberPromotionRule -> {
            MemberPromotionRuleResponse response = new MemberPromotionRuleResponse();
            response.setId(memberPromotionRule.getId());
            response.setExplain(memberPromotionRule.getExplain());
            response.setLevelTypeName(MemberLevelTypeEnum.getCodeMsg(memberPromotionRule.getType()));
            response.setPromotionRule(memberPromotionRule.getPromotionRule());
            response.setState(memberPromotionRule.getState());
            response.setType(memberPromotionRule.getType());
            return response;
        }).collect(Collectors.toList())));
    }

    @Override
    public Wrapper<Void> memberPromotionRuleUpdate(MemberPromotionRuleUpdateRequest request) {
        MemberPromotionRule memberPromotionRule = memberPromotionRuleRepository.findById(request.getId()).orElse(null);
        if (memberPromotionRule == null) {
            return Wrapper.fail("会员升级规则不存在");
        }

        //会员服务
        BaseLevelRuleStatusVO statusVO = new BaseLevelRuleStatusVO();
        statusVO.setMemberLevelTypeEnum(memberPromotionRule.getType());
        statusVO.setRuleTypeEnum(memberPromotionRule.getPromotionRule());
        statusVO.setStatus(request.getState());
        Wrapper<Void> result = baseLevelRightCreditControllerFeign.updateBaseLevelRuleConfigStatus(statusVO);
        if (result.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(result.getCode(), result.getMessage());
        }

        memberPromotionRule.setUpdateTime(System.currentTimeMillis());
        memberPromotionRule.setState(request.getState());
        memberPromotionRuleRepository.save(memberPromotionRule);
        return Wrapper.success();
    }
}
